import spock.lang.Specification

/**
 * Created at 2017/11/2 18:23
 * @author gongweixin
 */
class MapExample extends Specification {

    def "map 转换成新 map"() {
        expect:
        [1: [1,2], 2: [2,3]] == [1: [2,1], 2: [2,3]].collectEntries { key, value ->
            [key, value.sort()]
        }
    }

    def "循环的时候remove key"() {
        expect:
        def map = [1: 1, 2: 2, 3: 3]
        map.each { key, value ->
            if (key == 2) {
                map.remove(key)
            }
        }

        [1: 1, 3: 3] == map
    }

    def "循环的时候 put key value"() {
        expect:
        def map = [1: 1, 2: 2, 3: 3]
        map.each { key, value ->
            map[key] = value
        }

        [1: 1, 2: 2, 3: 3] == map
    }

    def "获取value中元素个数最多的key"() {
        expect:
        3 == [1: [1], 2: [1, 2], 3: [1,2,3]].max {it.value.size}.key
        2    == [1: [1], 2: [1, 2, 3], 3: [1,2,3]].max {it.value.size}.key
    }
}
